// var ClubLastInfo = 'SelectClub';
var ClubLastInfo = '';
var ClubMainWindow = Window.extend({
    _ccsFileName: res.ClubMainWindow_json,
    _clubList: [],
    _messagList: [],
    _tableView: null,
    initWindow: function () {
        //暂时先隐藏创建亲友圈按钮
        this._nodeList['btn_create'].setVisible(true);
        this._nodeList['btn_message'].setVisible(false);
        this._clubList = [0, 0, 0, 0, 0, 0, 0];
        this.initTableView();
        this._clubRefresh();
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'touchLayer': '_clickTouchLayer',
            'btn_close': '_clubClose',
            'btn_refresh': '_clubRefresh',
            'btn_help': '_clickHelp',
            'btn_message': '_clickMessage',
            'btn_create': '_clickCreate',
            'btn_join': '_clickJoin',
        }
    },
    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        this._eventListeners[ClubMsgCode.QueryMSG] = '_queryMSGHandelr';
        this._eventListeners[ClubMsgCode.QueryClubList] = '_queryClubListHandler';
        this._eventListeners[ClubEvent.ClubMainWindowClose] = 'close';
    },

    /**
     * 初始化TableView
     */
    initTableView: function () {
        if (this._tableView) return;
        var nodeContent = this._nodeList['nodeContent'];
        var tableViewSize = nodeContent.getContentSize();
        nodeContent.setContentSize(cc.size(display.width, tableViewSize.height));
        this._tableView = new cc.TableView(this, cc.size(display.width, tableViewSize.height));
        this._tableView.setAnchorPoint(cc.p(0.5, 0.5));
        this._tableView.setDelegate(this);
        this._tableView.setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL);
        this._tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this._tableView.y = nodeContent.y;
        this._tableView.x = -(display.width * 0.5) + 60;
        nodeContent.getParent().addChild(this._tableView);
    },
    /**
     * 点击刷新亲友圈
     * @private
     */
    _clubRefresh: function () {
        clubMsgManager.requestClubList(true);
        clubMsgManager.requestClubMsg();
    },

    _clubClose: function () {
        gameData.returnClubId = null;
        showBoard('HallBoard');
    },

    /**
     * 点击帮助
     * @private
     */
    _clickHelp: function () {
        this.addChild(new GuideLayer());
    },

    /**
     * 点击消息查看a
     * @private
     */
    _clickMessage: function () {
        showWindow('ClubMessageWindow', this._messagList);
        this.close();
    },

    /**
     * 点击创建亲友圈
     * @private
     */
    _clickCreate: function () {
        showWindow('ClubInputWindow', '请输入亲友圈名称', ClubInputWindowType.CreateClub,this.createClubHandler.bind(this));
    },

    /**
     * 点击加入亲友圈
     * @private
     */
    _clickJoin: function () {
        showWindow('ClubJoinOrInviteWindow', ClubJoinOrInviteWindow_Type.Join);
    },

    /**
     * 创建俱乐部 确定按钮 回调
     * @param inputStr
     */
    createClubHandler: function (inputStr) {
        network.send(2103, {cmd: ClubMsgCode.CreateClub, name: inputStr});
    },


    /**
     * 亲友圈基本数据
     * @private
     */
    _queryClubListHandler: function (data) {
        this._clubList = ClubDataManager.getAllClubData();
        this._refreshTableView();
    },

    /**
     * 刷新亲友圈item显示
     * @private
     */
    _refreshTableView: function () {
        var isHaveClub = this._clubList.length > 0;
        this._clubListSort();
        //this._nodeList['meinv'].setVisible(!isHaveClub);
        this._nodeList['meinv'].setVisible(false);
        this._tableView.reloadData();
    },

    /**
     * 亲友圈数据排序
     *
     * @private
     */
    _clubListSort: function () {
        //查找上次记录打开的亲友圈
        var clubId = cc.sys.localStorage.getItem(ClubLastInfo) || 0;
        if (clubId && clubId != 0) {
            var index = -1;
            for (var i = 0; i < this._clubList.length; i++) {
                if (this._clubList[i].id == clubId) {
                    index = i;
                }
            }
            if (index != -1) {
                var temp = this._clubList[0];
                this._clubList[0] = this._clubList[index];
                this._clubList[index] = temp;
            }
        }
    },


    /**
     * 亲友圈消息
     * @private
     */
    _queryMSGHandelr: function (data) {
        if (data.result != 0) {
            showAlert(data['msg']);
            return;
        }
        this._messagList = [];
        for (var i = 0; i < data['arr'].length; i++) {
            if (data['arr'][i].type == 'invite') {
                this._messagList.push(data['arr'][i]);
            }
        }
        if (this._messagList.length > 0) {
            this._nodeList['btn_message'].setVisible(true);
            this._nodeList['btn_message.numbg.num'].setString(this._messagList.length);
        } else {
            this._nodeList['btn_message'].setVisible(false);
        }
    },


    tableCellAtIndex: function (table, idx) {
        var cell = table.dequeueCell();
        var node = null;
        if (cell == null) {
            cell = new cc.TableViewCell();
            node = addItem('ClubMainItem', cell, this._clubList[idx],idx);
            node.setName("itemCell");
        } else {
            node = cell.getChildByName("itemCell");
            node.setData(this._clubList[idx],idx);
        }
        return cell;
    },
    tableCellTouched: function (table, cell) {

    },
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(360, 451);
    },
    numberOfCellsInTableView: function (table) {
        return this._clubList.length;
    },
    _clickTouchLayer: function () {

    },
    onExitTransitionDidStart: function () {
        cc.Node.prototype.onExitTransitionDidStart.call(this);
        // if (this._tableView) {
        //     this._tableView.removeFromParent(true);
        //     this._tableView = null;
        // }
        eventManager.removeListeners(this);
    },

    /**
     * View基类初始化
     */
    initView: function () {
        this._actionType = ViewActionType.None;
        if (this._actionType === ViewActionType.None) {
            this._rootNode.show();
        }
        this._loadCCSFinished = true;
        this.setData.apply(this, arguments);
    },

    open: function () {
        this._initAllListeners();
        this.initTableView();
        this._tableView.setTouchEnabled(true);
        clubMsgManager.requestClubList();
        clubMsgManager.requestClubMsg();
    },
});